import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login.vue";
import Home from "@/views/Home.vue";
import DeptView from "@/views/dept/DeptView.vue";
import EmpView from "@/views/emp/EmpView.vue";
import MealView from "@/views/meal/MealView.vue";
import OrderRecords from "@/views/orderRecords/OrderRecords.vue";
import OrderReview from "@/views/OrderReview/OrderReview.vue";
import OrderMain from "@/views/order/OrderMain.vue";
import UserView from "@/views/UserView.vue";
import orderSummary from "@/views/orderRecords/OrderSummary.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name:'home',
    redirect: 'orderMain',
    component: Home,
    children:[
      {
        path:"/emp",
        name:'emp',
        component:EmpView
      },
      {
        path: '/dept',
        name: 'dept',
        component: DeptView
      },
      {
        path: "/meal",
        name: "meal",
        component: MealView
      },
      {
        path: "/orderMain",
        name: "orderMain",
        component: OrderMain
      },
      {
        path: "/orderRecords",
        name: "orderRecords",
        component: OrderRecords
      },
      {
        path: "/orderReview",
        name:"orderReview",
        component: OrderReview
      },
      {
        path: "/userView",
        name:'userView',
        component: UserView
      },
      {
        path: "/orderSummary",
        name: orderSummary,
        component: orderSummary
      },
      {
        path: "",
        redirect: (to) => {
          const user = JSON.parse(sessionStorage.getItem("user"));
          const roleId = user.roleId;
          return roleId === 1 ? '/orderMain' : '/orderReview';
        },
      }
    ]
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next) => {
  if (to.name !== 'login' && sessionStorage.getItem("user") ==null){
    next({name:'login'});
  }else {
    next()//放行
  }
})
export default router
